

<!DOCTYPE html>
<html lang="zh-CN" data-default-color-scheme=&#34;auto&#34;>



<head>
  <meta charset="UTF-8">
  <link rel="apple-touch-icon" sizes="76x76" href="/img/favicon.ico">
  <link rel="icon" href="/img/favicon.ico">
  <meta name="viewport"
        content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, shrink-to-fit=no">
  <meta http-equiv="x-ua-compatible" content="ie=edge">
  
  <meta name="theme-color" content="#2f4154">
  <meta name="description" content="叶尘的技术升级小木屋">
  <meta name="author" content="叶尘">
  <meta name="keywords" content="">
  
  <title>18、集合 - 叶尘的技术升级小木屋</title>

  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css" />


  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/github-markdown-css@4.0.0/github-markdown.min.css" />
  <link  rel="stylesheet" href="/lib/hint/hint.min.css" />

  
    
    
      
      <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/highlight.js@10.7.2/styles/github-gist.min.css" />
    
  

  
    <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.css" />
  



<!-- 主题依赖的图标库，不要自行修改 -->

<link rel="stylesheet" href="//at.alicdn.com/t/font_1749284_ba1fz6golrf.css">



<link rel="stylesheet" href="//at.alicdn.com/t/font_1736178_kmeydafke9r.css">


<link  rel="stylesheet" href="/css/main.css" />

<!-- 自定义样式保持在最底部 -->

  
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/shubiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/gundongtiao.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/daziyanse.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/gh/bynotes/texiao/source/css/toubudaziji.css">
<link rel="stylesheet" href="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.css">



  <script id="fluid-configs">
    var Fluid = window.Fluid || {};
    var CONFIG = {"hostname":"www.yechen-blog.cn","root":"/","version":"1.8.11","typing":{"enable":true,"typeSpeed":70,"cursorChar":"_","loop":false},"anchorjs":{"enable":true,"element":"h1,h2,h3,h4,h5,h6","placement":"right","visible":"hover","icon":""},"progressbar":{"enable":true,"height_px":3,"color":"#29d","options":{"showSpinner":false,"trickleSpeed":100}},"copy_btn":true,"image_zoom":{"enable":true,"img_url_replace":["",""]},"toc":{"enable":true,"headingSelector":"h1,h2,h3,h4,h5,h6","collapseDepth":0},"lazyload":{"enable":true,"loading_img":"/img/loading.gif","onlypost":false,"offset_factor":2},"web_analytics":{"enable":false,"baidu":null,"google":null,"gtag":null,"tencent":{"sid":null,"cid":null},"woyaola":null,"cnzz":null,"leancloud":{"app_id":null,"app_key":null,"server_url":null}},"search_path":"/local-search.xml"};
  </script>
  <script  src="/js/utils.js" ></script>
  <script  src="/js/color-schema.js" ></script>
<meta name="generator" content="Hexo 5.4.0"></head>


<body>
  <header style="height: 70vh;">
    <nav id="navbar" class="navbar fixed-top  navbar-expand-lg navbar-dark scrolling-navbar">
  <div class="container">
    <a class="navbar-brand"
       href="/">&nbsp;<strong>YECHEN</strong>&nbsp;</a>

    <button id="navbar-toggler-btn" class="navbar-toggler" type="button" data-toggle="collapse"
            data-target="#navbarSupportedContent"
            aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="Toggle navigation">
      <div class="animated-icon"><span></span><span></span><span></span></div>
    </button>

    <!-- Collapsible content -->
    <div class="collapse navbar-collapse" id="navbarSupportedContent">
      <ul class="navbar-nav ml-auto text-center">
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/">
                <i class="iconfont icon-home-fill"></i>
                首页
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/categories/">
                <i class="iconfont icon-qrcode"></i>
                分类
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/tags/">
                <i class="iconfont icon-tags-fill"></i>
                标签
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/about/">
                <i class="iconfont icon-user-fill"></i>
                关于
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item">
              <a class="nav-link" href="/playlist/">
                <i class="iconfont icon-music"></i>
                音乐
              </a>
            </li>
          
        
          
          
          
          
            <li class="nav-item dropdown">
              <a class="nav-link dropdown-toggle" target="_self" role="button"
                 data-toggle="dropdown" aria-haspopup="true" aria-expanded="false">
                <i class="iconfont icon-books"></i>
                链接
              </a>
              <div class="dropdown-menu" aria-labelledby="navbarDropdown">
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/">
                    
                    主题博客
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/guide/">
                    
                    配置指南
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://hexo.fluid-dev.com/docs/icon/">
                    
                    图标用法
                  </a>
                
                  
                  
                  
                  <a class="dropdown-item" target="_blank" rel="noopener" href="https://www.bilibili.com">
                    
                    哔哩哔哩
                  </a>
                
              </div>
            </li>
          
        
        
          <li class="nav-item" id="search-btn">
            <a class="nav-link" target="_self" data-toggle="modal" data-target="#modalSearch">&nbsp;<i
                class="iconfont icon-search"></i>&nbsp;</a>
          </li>
        
        
          <li class="nav-item" id="color-toggle-btn">
            <a class="nav-link" target="_self">&nbsp;<i
                class="iconfont icon-dark" id="color-toggle-icon"></i>&nbsp;</a>
          </li>
        
      </ul>
    </div>
  </div>
</nav>

    <div class="banner" id="banner" parallax=true
         style="background: url('/img/banner.jpg') no-repeat center center;
           background-size: cover;">
      <div class="full-bg-img">
        <div class="mask flex-center" style="background-color: rgba(0, 0, 0, 0.3)">
          <div class="page-header text-center fade-in-up">
            <span class="h2" id="subtitle" title="18、集合">
              
            </span>

            
              <div class="mt-3">
  
    <span class="post-meta mr-2">
      <i class="iconfont icon-author" aria-hidden="true"></i>
      叶尘
    </span>
  
  
    <span class="post-meta">
      <i class="iconfont icon-date-fill" aria-hidden="true"></i>
      <time datetime="2021-09-13 22:20" pubdate>
        星期一, 九月 13日 2021, 10:20 晚上
      </time>
    </span>
  
</div>

<div class="mt-1">
  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-chart"></i>
      6.7k 字
    </span>
  

  
    
    <span class="post-meta mr-2">
      <i class="iconfont icon-clock-fill"></i>
      
      
      89
       分钟
    </span>
  

  
  
    
      <!-- LeanCloud 统计文章PV -->
      <span id="leancloud-page-views-container" class="post-meta" style="display: none">
        <i class="iconfont icon-eye" aria-hidden="true"></i>
        <span id="leancloud-page-views"></span> 次
      </span>
    
  
</div>

            
          </div>

          
        </div>
      </div>
    </div>
  </header>

  <main>
    
      

<div class="container-fluid nopadding-x">
  <div class="row nomargin-x">
    <div class="d-none d-lg-block col-lg-2"></div>
    <div class="col-lg-8 nopadding-x-md">
      <div class="container nopadding-x-md" id="board-ctn">
        <div class="py-5" id="board">
          <article class="post-content mx-auto">
            <!-- SEO header -->
            <h1 style="display: none">18、集合</h1>
            
            <div class="markdown-body">
              <h3 id="18-1-集合概述"><a href="#18-1-集合概述" class="headerlink" title="18.1 集合概述"></a>18.1 集合概述</h3><h4 id="18-1-1-什么是集合"><a href="#18-1-1-什么是集合" class="headerlink" title="18.1.1 什么是集合"></a>18.1.1 什么是集合</h4><ul>
<li><p>集合实际上就是一个**==容器==**。可以来容纳其它类型的数据。</p>
</li>
<li><p>集合不能直接存储基本数据类型，另外集合也不能直接存储java对象，</p>
<p>**==集合当中存储的都是java对象的内存地址==**。（或者说集合中存储的是引用。）</p>
</li>
<li><p>集合在java中本身是一个容器，是一个**==对象==**</p>
</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20210218120728216.png" srcset="/img/loading.gif" lazyload alt="001-集合中存储的是对象的内存地址"></p>
<h4 id="18-1-2-在java中每一个不同的集合，底层会对应不同的数据结构"><a href="#18-1-2-在java中每一个不同的集合，底层会对应不同的数据结构" class="headerlink" title="18.1.2 在java中每一个不同的集合，底层会对应不同的数据结构"></a>18.1.2 在java中每一个不同的集合，底层会对应不同的数据结构</h4><figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">new</span> ArratList(); <span class="hljs-comment">// 创建一个集合对象，底层是数组</span><br><span class="hljs-keyword">new</span> LinkList();<span class="hljs-comment">// 创建一个集合对象，底层是链表</span><br><span class="hljs-keyword">new</span> TreeSet();<span class="hljs-comment">// 创建一个集合对象，底层是二叉树</span><br></code></pre></td></tr></table></figure>

<h4 id="18-1-3-集合继承结构"><a href="#18-1-3-集合继承结构" class="headerlink" title="18.1.3 集合继承结构"></a>18.1.3 集合继承结构</h4><p><strong>java 中集合分为两类：</strong></p>
<ul>
<li>一类是==单个方式==存储元素： </li>
</ul>
<p>单个方式存储元素，这一类集合中<strong>超级父接口：java.util.Collection</strong>;</p>
<p> <img src="https://cdn.jsdelivr.net/gh/yechen-ops/image-hosting@master/20211009/gaitubao_20210218131441367_jpg.jpg" srcset="/img/loading.gif" lazyload alt="Collection集合继承结构图"></p>
<ul>
<li><p>一类是以==键值对儿的方式==存储元素 </p>
<p>以键值对的方式存储元素，这一类集合中<strong>超级父接口：java.util.Map</strong>;</p>
</li>
</ul>
<p> <img src="https://cdn.jsdelivr.net/gh/yechen-ops/image-hosting@master/20211009/gaitubao_2021021813340813_jpg.jpg" srcset="/img/loading.gif" lazyload alt="Map集合的继承结构图"></p>
<p><strong>总结：</strong></p>
<p><strong>实现类：</strong></p>
<blockquote>
<p><strong>[ArrayList](####18.3.1 ArrayList)：底层是数组；</strong></p>
<p><strong>[LinkedList](####18.3.2 LinkList )：底层是双向链表；</strong></p>
<p><strong>[Vector](####18.3.3 Vector（不常用）)：底层是数组，是线程安全的，效率较低，使用较少；</strong></p>
<p><strong>[HashSet](####18.6.2 HashSet / HashMap )：底层使用的是 HashMap，放到 HashSet 中的元素等同于放到 HashMap 集合中的 key 部分了；</strong></p>
<p><strong>[TreeSet](####18.6.5 TreeSet / TreeMap)：底层是 TreeMap，放到 TreeSet 集合中的元素等同于放到 TreeMap 集合中的 key 部分了；</strong></p>
<p><strong>[HashMap](####18.6.2 HashSet / HashMap )：底层是哈希表；</strong></p>
<p><strong>[HashTable](####18.6.3 HashTable)：底层是哈希表，只不过是线程安全的，效率较低，使用较少；</strong></p>
<p><strong>[Properties](####18.6.4 Properties)：是 HashTable 的子类，是线程安全的，并且 key 和 value 只能存储字符串 String 类型；</strong></p>
<p><strong>[TreeMap](####18.6.5 TreeSet / TreeMap)：底层是二叉树，TreeMap 集合的 key 可以自动按照大小顺序排序。</strong></p>
</blockquote>
<p><strong>[List](####18.3.1 List 接口) 集合存储元素的特点：有序可重复</strong></p>
<blockquote>
<p>有序：存进去的顺序和取出来的顺序相同，每一个元素都有下标。</p>
<p>可重复：可以存进去两个相同的元素，例如：存进去 1，可以再存进去 1。</p>
</blockquote>
<p><strong>[Set（Map）](####18.6.1 Map 接口)集合存储元素的特点：无序不可重复</strong></p>
<blockquote>
<p>无序：存进去的顺序和取出来的顺序不一定相同，另外 Set 集合中的元素没有下标。</p>
<p>不可重复：不可以存进去两个相同的元素。</p>
</blockquote>
<p><strong>SortedSet（SortedMap）集合存储元素的特点：无序不可重复，元素可排序</strong></p>
<blockquote>
<p>无序：存进去的顺序和取出来的顺序不一定相同，另外 Set 集合中的元素没有下标。</p>
<p>不可重复：不可以存进去两个相同的元素。</p>
<p>可排序：可以按照 Key 的大小顺序排序。</p>
</blockquote>
<p><strong>Map集合中的Key，就是一个Set集合。</strong></p>
<p><strong>往Set集合中放数据，实际上放到了Map集合的Key部分。</strong></p>
<h3 id="18-2-Collection-和-Iterator"><a href="#18-2-Collection-和-Iterator" class="headerlink" title="18.2 Collection 和 Iterator"></a>18.2 Collection 和 Iterator</h3><h4 id="18-2-1-Collection集合"><a href="#18-2-1-Collection集合" class="headerlink" title="18.2.1 Collection集合"></a>18.2.1 Collection集合</h4><p><strong>Collection 中可以存放什么元素：</strong></p>
<blockquote>
<p>没有使用“泛型”之前，Collection中可以存储Object的所有子类型。</p>
<p>使用了“泛型”之后，Collection中只能存储某个具体的类型。</p>
<p>集合中不能直接存储基本数据类型，也不能存 java 对象，只是存储 java 对象的内存地址。</p>
</blockquote>
<p><strong>Collection 中的常用方法：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">add</span><span class="hljs-params">(Object e)</span> <span class="hljs-comment">// 向集合中 末尾 添加元素</span></span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">size</span><span class="hljs-params">()</span>  <span class="hljs-comment">// 获取集合中元素的个数</span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">clear</span><span class="hljs-params">()</span> <span class="hljs-comment">// 清空集合</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">contains</span><span class="hljs-params">(Object o)</span> <span class="hljs-comment">// 判断当前集合中是否包含元素o，包含返回true，不包含返回false</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">remove</span><span class="hljs-params">(Object o)</span> <span class="hljs-comment">// 删除集合中的某个元素。</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">()</span>  <span class="hljs-comment">// 判断该集合中元素的个数是否为0</span></span><br><span class="hljs-function">Object[] <span class="hljs-title">toArray</span><span class="hljs-params">()</span>  <span class="hljs-comment">// 调用这个方法可以把集合转换成数组。【作为了解，使用不多。】</span></span><br></code></pre></td></tr></table></figure>

<p><strong>contains源码分析</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 测试程序</span><br>Collection collection = <span class="hljs-keyword">new</span> ArrayList();<br><br>String s1 = <span class="hljs-keyword">new</span> String(<span class="hljs-string">&quot;abc&quot;</span>);<br>String s2 = <span class="hljs-keyword">new</span> String(<span class="hljs-string">&quot;def&quot;</span>);<br>String s3 = <span class="hljs-keyword">new</span> String(<span class="hljs-string">&quot;abc&quot;</span>);<br><br>collection.add(s1);<br>collection.add(s2);<br><br><span class="hljs-keyword">boolean</span> result = collection.contains(s3);<br>System.out.println(result);<span class="hljs-comment">// 输出：true</span><br></code></pre></td></tr></table></figure>

<p><img src="https://img-blog.csdnimg.cn/2021021815361690.png" srcset="/img/loading.gif" lazyload alt="image-20210218153600561"></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 源码</span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">contains</span><span class="hljs-params">(Object o)</span> </span>&#123;<br>    <span class="hljs-comment">// 如果indexOf方法返回的值大于等于 0，就返回 true，表示包含</span><br>    <span class="hljs-keyword">return</span> indexOf(o) &gt;= <span class="hljs-number">0</span>;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">indexOf</span><span class="hljs-params">(Object o)</span> </span>&#123;<br>    <span class="hljs-comment">// 如果传进来的元素是 null</span><br>    <span class="hljs-keyword">if</span> (o == <span class="hljs-keyword">null</span>) &#123;<br>        <span class="hljs-comment">// 编历数组，存在 null值就返回下标</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size; i++)<br>            <span class="hljs-keyword">if</span> (elementData[i]==<span class="hljs-keyword">null</span>)<br>                <span class="hljs-keyword">return</span> i;<br>    &#125; <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-comment">// 编历数组</span><br>        <span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; size; i++)<br>            <span class="hljs-comment">// 使用 equals() 来判断对象中的内容是否相同，不以内存地址为判断标准</span><br>            <span class="hljs-comment">// 当然前提条件是当前类重写了 equals() 方法，否则比较的还是内存地址</span><br>            <span class="hljs-keyword">if</span> (o.equals(elementData[i]))<br>                <span class="hljs-keyword">return</span> i;<br>    &#125;<br>    <span class="hljs-keyword">return</span> -<span class="hljs-number">1</span>;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>结论：contains方法中是使用 equals()  方法来判断两个对象是否相等的，所有存放在 集合中的对象一定要重写 equals() 方法。</strong></p>
<p>​            <strong>remove方法底层也是调用 equals() 方法来判断两个对象是否相等的。</strong></p>
<h4 id="18-2-2-Iterator迭代器"><a href="#18-2-2-Iterator迭代器" class="headerlink" title="18.2.2 Iterator迭代器"></a><strong>18.2.2 Iterator迭代器</strong></h4><p>==Collection 接口继承 Iterable 接口，继承了 Iterator() 方法，会返回一个 Iterator 类型的迭代器，用于 Collection 的编历。（在 Map 中不能用）==</p>
<p><strong>迭代器 Iterator 中的方法：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">hasNext</span><span class="hljs-params">()</span><span class="hljs-comment">// 如果仍有元素可以迭代，则返回 true。</span></span><br><span class="hljs-function">Object <span class="hljs-title">next</span><span class="hljs-params">()</span><span class="hljs-comment">// 返回迭代的下一个元素。</span></span><br></code></pre></td></tr></table></figure>

<p><strong>迭代器执行过程：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 创建集合</span><br>Collection collection = <span class="hljs-keyword">new</span> ArrayList();<br><span class="hljs-comment">// 添加元素</span><br>collection.add(<span class="hljs-number">100</span>);<br>collection.add(<span class="hljs-number">3.14</span>);<br>collection.add(<span class="hljs-string">&quot;abc&quot;</span>);<br>collection.add(<span class="hljs-keyword">new</span> Object());<br><span class="hljs-comment">// 使用Iterator迭代器对集合编历</span><br><span class="hljs-comment">// 第一步：获取集合迭代器对象</span><br>Iterator iterator = collection.iterator();<br><span class="hljs-comment">// 第二步：通过迭代器对象开始编历集合</span><br><span class="hljs-keyword">while</span> (iterator.hasNext()) &#123;<br>    Object obj = iterator.next();<br>    System.out.println(obj);<br>&#125;<br></code></pre></td></tr></table></figure>



<p><img src="https://img-blog.csdnimg.cn/20210218143913522.png" srcset="/img/loading.gif" lazyload alt="image-20210218143857008"></p>
<ul>
<li><p><strong>迭代集合过程中用 remove() 方法会出现异常，要使用迭代器的 remove 方法</strong></p>
<p>在迭代集合元素的过程中，不能调用集合对象的 remove 方法，删除元素：<code>collection.remove(o)</code>; 迭代过程中不能这样。否则会出现：<code>java.util.ConcurrentModificationException</code> (<strong>当集合的结构发生改变时，迭代器必须重新获取</strong>)</p>
<p><strong>在迭代元素的过程当中，一定要使用迭代器 Iterator 的 remove 方法，删除元素，不要使用集合自带的remove方法删除元素。</strong></p>
<p><a target="_blank" rel="noopener" href="https://www.bilibili.com/video/BV1Rx411876f?p=677">视频讲解</a></p>
</li>
</ul>
<h3 id="18-3-List-接口及其实现类"><a href="#18-3-List-接口及其实现类" class="headerlink" title="18.3 List 接口及其实现类"></a>18.3 List 接口及其实现类</h3><h4 id="18-3-1-List-接口"><a href="#18-3-1-List-接口" class="headerlink" title="18.3.1 List 接口"></a>18.3.1 List 接口</h4><p><strong>List 集合存储元素特点：有序可重复</strong></p>
<p><strong>List 接口特有的方法：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index, Object element)</span><span class="hljs-comment">// 在列表指定位置（index）处插入元素（element），效率较低，使用较少</span></span><br><span class="hljs-function">Object <span class="hljs-title">set</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index, Object element)</span><span class="hljs-comment">// 修改指定位置（index）处元素为（element），返回被替换的元素</span></span><br><span class="hljs-function">Object <span class="hljs-title">get</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index)</span><span class="hljs-comment">// 根据下标（index）获取指定元素</span></span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">indexOf</span><span class="hljs-params">(Object o)</span><span class="hljs-comment">// 获取指定对象（o）第一次出现处的索引</span></span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">lastIndexOf</span><span class="hljs-params">(Object o)</span><span class="hljs-comment">// 获取指定对象（o）最后一次出现处的索引</span></span><br><span class="hljs-function">Object <span class="hljs-title">remove</span><span class="hljs-params">(<span class="hljs-keyword">int</span> index)</span><span class="hljs-comment">// 删除指定位置（index）处的元素，返回删除的元素</span></span><br></code></pre></td></tr></table></figure>

<p><strong>由于有下标，所以 List 集合有自己特有的编历方式</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br></pre></td><td class="code"><pre><code class="hljs java">List list = <span class="hljs-keyword">new</span> ArrayList();<br>list.add(<span class="hljs-number">100</span>);<br>list.add(<span class="hljs-number">110</span>);<br>list.add(<span class="hljs-number">120</span>);<br><br><span class="hljs-comment">// 编历</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; list.size(); i++) &#123;<br>    System.out.println(list.get(i));<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="18-3-1-ArrayList"><a href="#18-3-1-ArrayList" class="headerlink" title="18.3.1 ArrayList"></a>18.3.1 ArrayList</h4><p>==默认初始化容量为 10（底层先创建了一个长度为 0 的数组，当添加第一个元素的时候，初始哈容量 10）。==</p>
<p>ArrayList 底层是一个 <strong>Object[] 数组</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">transient</span> Object[] elementData;<br></code></pre></td></tr></table></figure>

<p><strong>ArrayList 的无参构造方法会初始化一个长度为0的数组，在添加元素的时候会初始化容量。</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">private</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">final</span> Object[] DEFAULTCAPACITY_EMPTY_ELEMENTDATA = <br><span class="hljs-comment">//     </span><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ArrayList</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.elementData = DEFAULTCAPACITY_EMPTY_ELEMENTDATA;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>ArrayList 的有参构造需要传进去一个 int 类型的初始化容量（ininialCapacity）</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">ArrayList</span><span class="hljs-params">(<span class="hljs-keyword">int</span> initialCapacity)</span> </span>&#123;<br>    <span class="hljs-keyword">if</span> (initialCapacity &gt; <span class="hljs-number">0</span>) &#123;<br>        <span class="hljs-keyword">this</span>.elementData = <span class="hljs-keyword">new</span> Object[initialCapacity];<br>    &#125; <span class="hljs-keyword">else</span> <span class="hljs-keyword">if</span> (initialCapacity == <span class="hljs-number">0</span>) &#123;<br>        <span class="hljs-keyword">this</span>.elementData = EMPTY_ELEMENTDATA;<br>    &#125; <span class="hljs-keyword">else</span> &#123;<br>        <span class="hljs-keyword">throw</span> <span class="hljs-keyword">new</span> IllegalArgumentException(<span class="hljs-string">&quot;Illegal Capacity: &quot;</span>+<br>                                           initialCapacity);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>数组每次扩容会扩大之前容量的 1.5 倍</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">private</span> <span class="hljs-keyword">void</span> <span class="hljs-title">grow</span><span class="hljs-params">(<span class="hljs-keyword">int</span> minCapacity)</span> </span>&#123;<br>    <span class="hljs-comment">// overflow-conscious code</span><br>    <span class="hljs-keyword">int</span> oldCapacity = elementData.length;<br>    <span class="hljs-keyword">int</span> newCapacity = oldCapacity + (oldCapacity &gt;&gt; <span class="hljs-number">1</span>);<span class="hljs-comment">// 新容量是老容量的 1.5 倍</span><br>    <span class="hljs-keyword">if</span> (newCapacity - minCapacity &lt; <span class="hljs-number">0</span>)<br>        newCapacity = minCapacity;<br>    <span class="hljs-keyword">if</span> (newCapacity - MAX_ARRAY_SIZE &gt; <span class="hljs-number">0</span>)<br>        newCapacity = hugeCapacity(minCapacity);<br>    <span class="hljs-comment">// minCapacity is usually close to size, so this is a win:</span><br>    elementData = Arrays.copyOf(elementData, newCapacity);<br>&#125;<br></code></pre></td></tr></table></figure>

<p>==二进制位运算：==</p>
<p>&gt;&gt; n ： ==表示二进制向右移动 n 位，计算成十进制就是 缩小为原来的  $ \frac{1}{2^n}$ 倍==</p>
<p>&lt;&lt; n ： ==表示二进制向左移动 n 位，计算成十进制就是 扩大为原来的  2^n^ 倍==</p>
<h4 id="18-3-2-LinkList"><a href="#18-3-2-LinkList" class="headerlink" title="18.3.2 LinkList"></a>18.3.2 LinkList</h4><p><strong>链表的优点</strong></p>
<blockquote>
<p>由于链表上的元素在空间存储上内存地址不连续。</p>
<p>所以随机增删元素的时候不会有大量元素位移，因此随机增删效率较高。</p>
<p>在以后的开发中，如果遇到随机增删集合中元素的业务比较多时，建议使用LinkedList。</p>
</blockquote>
<p><strong>链表的缺点</strong></p>
<blockquote>
<p>不能通过数学表达式计算被查找元素的内存地址，每一次查找都是从头</p>
<p>节点开始遍历，直到找到为止。所以LinkedList集合检索/查找的效率较低。</p>
</blockquote>
<p><strong>单链表</strong> </p>
<blockquote>
<p>节点（Node）是单链表中基本的单元，每一个节点都有两个属性</p>
<p>一个属性是存储数据的</p>
<p>一个属性是存储下一个节点的内存地址的</p>
</blockquote>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 节点</span><br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span> </span>&#123;<br>    <span class="hljs-comment">// 存储数据</span><br>    Object data;<br>    <span class="hljs-comment">// 下一个节点</span><br>    Node next;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Node</span><span class="hljs-params">(Object data, Node next)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.data = data;<br>        <span class="hljs-keyword">this</span>.next = next;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Link</span> </span>&#123;<br>    <span class="hljs-comment">// 头节点</span><br>    Node node;<br>    <br>    <span class="hljs-comment">// 向链表末尾添加节点的方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">add</span><span class="hljs-params">(Object data)</span> </span>&#123;<br>        <span class="hljs-comment">// 如果头节点为 null</span><br>        <span class="hljs-keyword">if</span> (node == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-comment">// 新建一个头节点</span><br>            node = <span class="hljs-keyword">new</span> Node(data, <span class="hljs-keyword">null</span>);<br>        &#125; <span class="hljs-keyword">else</span> &#123;<span class="hljs-comment">// 头节点不是 null</span><br>            <span class="hljs-comment">// 找到最后一个节点</span><br>            Node lastNode = findList(node);<br>            <span class="hljs-comment">// 让尾节点的next执行新节点</span><br>            lastNode.next = <span class="hljs-keyword">new</span> Node(data, <span class="hljs-keyword">null</span>);<br>        &#125;<br>    &#125;<br><br>    <span class="hljs-function"><span class="hljs-keyword">private</span> Node <span class="hljs-title">findList</span><span class="hljs-params">(Node node)</span> </span>&#123;<br>        <span class="hljs-comment">// 如果这个节点的next是null，表明这个节点就是尾结点了</span><br>        <span class="hljs-keyword">if</span> (node.next == <span class="hljs-keyword">null</span>) &#123;<br>            <span class="hljs-keyword">return</span> node;<br>        &#125;<br>        <span class="hljs-comment">// 使用递归</span><br>        <span class="hljs-keyword">return</span> findList(node.next);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>LinkedList（==双向链表==）</strong></p>
<p>add方法源码阅读</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">LinkedList</span><span class="hljs-params">()</span> </span>&#123;<br>	Node&lt;E&gt; first = <span class="hljs-keyword">null</span>;<span class="hljs-comment">// 指向当前链表的第一个节点</span><br>    Node&lt;E&gt; last = <span class="hljs-keyword">null</span>;<span class="hljs-comment">// 指向当前链表的最后一个节点</span><br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">boolean</span> <span class="hljs-title">add</span><span class="hljs-params">(E e)</span> </span>&#123;<br>    linkLast(e);<br>    <span class="hljs-keyword">return</span> <span class="hljs-keyword">true</span>;<br>&#125;<br><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">linkLast</span><span class="hljs-params">(E e)</span> </span>&#123;<br>    <span class="hljs-keyword">final</span> Node&lt;E&gt; l = last;<span class="hljs-comment">// 使 l 执行当前链表中的最后一个节点</span><br>    <span class="hljs-keyword">final</span> Node&lt;E&gt; newNode = <span class="hljs-keyword">new</span> Node&lt;&gt;(l, e, <span class="hljs-keyword">null</span>);<span class="hljs-comment">// 创建一个新的节点对象，prev 指向最后一个节点，next 指向 null</span><br>    last = newNode;<span class="hljs-comment">// 使 last 指向新创建的节点（此时新节点就是链表中最后的一个节点了）</span><br>    <span class="hljs-keyword">if</span> (l == <span class="hljs-keyword">null</span>)<span class="hljs-comment">// 这个 if 分支只会执行在当前链表中没有节点的时候</span><br>        first = newNode;<span class="hljs-comment">// 使 first 指向新创建的第一个节点</span><br>    <span class="hljs-keyword">else</span><br>        l.next = newNode;<span class="hljs-comment">// 使之前最后一个节点的 next 指向新建的节点</span><br>    size++;<br>    modCount++;<br>&#125;<br></code></pre></td></tr></table></figure>



<h4 id="18-3-3-Vector（不常用）"><a href="#18-3-3-Vector（不常用）" class="headerlink" title="18.3.3 Vector（不常用）"></a>18.3.3 Vector（不常用）</h4><p>底层也是一个数组</p>
<p>初始化容量为 10</p>
<p>扩容后的容量是之前的 2 倍</p>
<p>Vector 中的所有方法都是线程同步的，都带有 syschronized 关键字，是线程安全的，效率比较低，使用较少了</p>
<p>怎样将一个线程不安全的 ArrayList 集合装换为线程安全的呢？</p>
<ul>
<li>使用工具类 <code>java.util.Collections;</code></li>
<li><code>Collections.synchronizedList(List&lt;T&gt; list)</code></li>
</ul>
<h3 id="18-4-泛型机制"><a href="#18-4-泛型机制" class="headerlink" title="18.4 泛型机制"></a>18.4 泛型机制</h3><p><strong>JDK5.0之后推出的新特性：泛型</strong></p>
<p>泛型这种语法机制，只在程序编译阶段起作用，只是给编译器参考的。（运行阶段泛型没用！）</p>
<p>泛型好处：</p>
<ol>
<li>集合中存储的元素类型统一了。</li>
<li>从集合中取出的元素类型是泛型指定的类型，不需要进行大量的“向下转型”！</li>
</ol>
<p>泛型缺点：</p>
<ol>
<li>导致集合中存储的元素缺乏多样性；</li>
<li>大多数业务中，集合中元素的类型还是统一的。所以这种泛型特性被大家所认可。</li>
</ol>
<p>泛型实例：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br><span class="line">47</span><br><span class="line">48</span><br><span class="line">49</span><br><span class="line">50</span><br><span class="line">51</span><br><span class="line">52</span><br><span class="line">53</span><br><span class="line">54</span><br><span class="line">55</span><br><span class="line">56</span><br><span class="line">57</span><br><span class="line">58</span><br><span class="line">59</span><br><span class="line">60</span><br><span class="line">61</span><br><span class="line">62</span><br><span class="line">63</span><br><span class="line">64</span><br><span class="line">65</span><br><span class="line">66</span><br><span class="line">67</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        <span class="hljs-comment">// 使用泛型</span><br>        <span class="hljs-comment">// 使用泛型List&lt;Animal&gt;之后，表示List集合中只能存储Animal类型的数据</span><br>        <span class="hljs-comment">// 用泛型来指定集合中存储的数据类型</span><br>        <span class="hljs-comment">// 指定List集合中只能存储Animal类型数据，存储String类型数据编译就报错了</span><br>        <span class="hljs-comment">// 这样使用了泛型后，集合中的元素的数据类型更加统一了</span><br>        List&lt;Animal&gt; myList = <span class="hljs-keyword">new</span> ArrayList&lt;Animal&gt;();<br><br>        <span class="hljs-comment">// 准备对象</span><br>        Cat cat = <span class="hljs-keyword">new</span> Cat();<br>        Bird bird = <span class="hljs-keyword">new</span> Bird();<br><br>        <span class="hljs-comment">// 将对象添加到集合中去</span><br>        myList.add(cat);<br>        myList.add(bird);<br><br>        <span class="hljs-comment">// 编历集合，取出Cat让它抓老鼠，取出Bird让它飞翔</span><br>        <span class="hljs-comment">// 获取迭代器,表示迭代器迭代的是Animal类型</span><br>        Iterator&lt;Animal&gt; iterator = myList.iterator();<br>        <span class="hljs-keyword">while</span> (iterator.hasNext()) &#123;<br>            <span class="hljs-comment">// 使用泛型后，每一次迭代返回的数据都是Animal类型</span><br>            Animal animal = iterator.next();<br>            <span class="hljs-comment">// 这里就不需要进行强制类型转换了</span><br>            animal.move();<br>            <span class="hljs-comment">// 但是如果要调用子类特有的方法，就需要向下转型（是一定要向下转型的）</span><br>            <span class="hljs-keyword">if</span> (animal <span class="hljs-keyword">instanceof</span> Cat) &#123;<br>                ((Cat) animal).catchMouse();<br>            &#125;<br>            <span class="hljs-keyword">if</span> (animal <span class="hljs-keyword">instanceof</span> Bird) &#123;<br>                ((Bird) animal).catchChicken();<br>            &#125;<br><br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Animal</span> </span>&#123;<br>    <span class="hljs-comment">// 父类特有方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">move</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;动物在移动&quot;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Cat</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Animal</span> </span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">move</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;猫在走猫步&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// 子类特有方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">catchMouse</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;猫在捉老鼠&quot;</span>);<br>    &#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Bird</span> <span class="hljs-keyword">extends</span> <span class="hljs-title">Animal</span> </span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">move</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;鸟儿在飞翔&quot;</span>);<br>    &#125;<br><br>    <span class="hljs-comment">// 子类特有方法</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">catchChicken</span><span class="hljs-params">()</span> </span>&#123;<br>        System.out.println(<span class="hljs-string">&quot;鸟儿在捉小鸡&quot;</span>);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p>自定义泛型：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>       <span class="hljs-comment">// 这是钻石表达式，前面指定了类型，后面就可以不指定了</span><br>       MyClass&lt;String&gt; MyClass01 = <span class="hljs-keyword">new</span> MyClass&lt;&gt;();<br>        MyClass01.doSome(<span class="hljs-string">&quot;e&quot;</span>);<br>        <span class="hljs-comment">// 以下会报错</span><br>        <span class="hljs-comment">// stringMyClass.doSome(123);</span><br><br>        MyClass&lt;Integer&gt; myClass02 = <span class="hljs-keyword">new</span> MyClass&lt;&gt;();<br>        myClass02.doSome(<span class="hljs-number">123</span>);<span class="hljs-comment">// 自动装箱</span><br>        <span class="hljs-comment">// 以下会报错</span><br>        <span class="hljs-comment">// myClass.doSome(true);</span><br><br>        <span class="hljs-comment">// 不使用泛型，默认类型为 Object</span><br>        MyClass myClass03 = <span class="hljs-keyword">new</span> MyClass();<br>        myClass03.doSome(<span class="hljs-keyword">new</span> Object());<br>    &#125;<br>&#125;<br><br><span class="hljs-comment">// 当前类使用了泛型</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">MyClass</span>&lt;<span class="hljs-title">E</span>&gt; </span>&#123;<br>    <span class="hljs-comment">// 方法内传进去的参数类型为 E，在 new 对象的时候可以规定下来，不使用泛型，就默认为 Object</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">void</span> <span class="hljs-title">doSome</span><span class="hljs-params">(E e)</span> </span>&#123;<br>        System.out.println(e);<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="18-5-增强-for-循环（foreach）"><a href="#18-5-增强-for-循环（foreach）" class="headerlink" title="18.5 增强 for 循环（foreach）"></a>18.5 增强 for 循环（foreach）</h3><p><strong>JDK5.0后推出了一个新特性：叫做增强for循环，或者叫做foreach</strong></p>
<p>**foreach有一个缺点：==没有下标。在需要使用下标的循环中，不建议使用增强for循环==**。</p>
<p>增强 for 循环编历数组：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">int</span>[] array = &#123;<span class="hljs-number">1</span>,<span class="hljs-number">3</span>,<span class="hljs-number">54</span>,<span class="hljs-number">5657</span>,<span class="hljs-number">34</span>,<span class="hljs-number">4664</span>,<span class="hljs-number">43</span>&#125;;<br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i : array) &#123;<br>    System.out.println(i);<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">	输出：</span><br><span class="hljs-comment">		1</span><br><span class="hljs-comment">        3</span><br><span class="hljs-comment">        54</span><br><span class="hljs-comment">        5657</span><br><span class="hljs-comment">        34</span><br><span class="hljs-comment">        4664</span><br><span class="hljs-comment">        43</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p>增强 for 循环编历集合：</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 三种方式编历集合</span><br><span class="hljs-comment">// 创建集合</span><br>List&lt;String&gt; list = <span class="hljs-keyword">new</span> ArrayList&lt;&gt;();<br>list.add(<span class="hljs-string">&quot;zhangsan&quot;</span>);<br>list.add(<span class="hljs-string">&quot;lisi&quot;</span>);<br>list.add(<span class="hljs-string">&quot;wangwu&quot;</span>);<br>list.add(<span class="hljs-string">&quot;zhaoliu&quot;</span>);<br><br><span class="hljs-comment">// 使用迭代器方式</span><br>Iterator&lt;String&gt; iterator = list.iterator();<br><span class="hljs-keyword">while</span> (iterator.hasNext()) &#123;<br>    String next = iterator.next();<br>    System.out.println(next);<br>&#125;<br><br>System.out.println(<span class="hljs-string">&quot;============&quot;</span>);<br><br><span class="hljs-comment">// 使用普通 for 循环，通过下标方式</span><br><span class="hljs-keyword">for</span> (<span class="hljs-keyword">int</span> i = <span class="hljs-number">0</span>; i &lt; list.size(); i++) &#123;<br>    String string = list.get(i);<br>    System.out.println(string);<br>&#125;<br><br>System.out.println(<span class="hljs-string">&quot;============&quot;</span>);<br><br><span class="hljs-comment">// 使用增强 for 循环</span><br><span class="hljs-keyword">for</span> (String s : list) &#123;<br>    System.out.println(s);<br>&#125;<br></code></pre></td></tr></table></figure>



<h3 id="18-6-Map-接口及其实现类"><a href="#18-6-Map-接口及其实现类" class="headerlink" title="18.6 Map 接口及其实现类"></a>18.6 Map 接口及其实现类</h3><h4 id="18-6-1-Map-接口"><a href="#18-6-1-Map-接口" class="headerlink" title="18.6.1 Map 接口"></a>18.6.1 Map 接口</h4><p>Map和Collection没有继承关系</p>
<p>Map集合以==key 和 value==的方式存储数据：==键值对==</p>
<ul>
<li>key和value都是引用数据类型。</li>
<li>key和value都是存储对象的内存地址。</li>
<li>key起到主导的地位，value是key的一个附属品。</li>
</ul>
<p><strong>Map接口中常用方法：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-function">V <span class="hljs-title">put</span><span class="hljs-params">(K key, V value)</span> <span class="hljs-comment">// 向Map集合中添加键值对</span></span><br><span class="hljs-function">V <span class="hljs-title">get</span><span class="hljs-params">(Object key)</span> <span class="hljs-comment">// 通过key获取value</span></span><br><span class="hljs-function"><span class="hljs-keyword">void</span> <span class="hljs-title">clear</span><span class="hljs-params">()</span>    <span class="hljs-comment">// 清空Map集合</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">containsKey</span><span class="hljs-params">(Object key)</span> <span class="hljs-comment">// 判断Map中是否包含某个key</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">containsValue</span><span class="hljs-params">(Object value)</span> <span class="hljs-comment">// 判断Map中是否包含某个value</span></span><br><span class="hljs-function"><span class="hljs-keyword">boolean</span> <span class="hljs-title">isEmpty</span><span class="hljs-params">()</span>   <span class="hljs-comment">// 判断Map集合中元素个数是否为0</span></span><br><span class="hljs-function">V <span class="hljs-title">remove</span><span class="hljs-params">(Object key)</span> <span class="hljs-comment">// 通过key删除键值对</span></span><br><span class="hljs-function"><span class="hljs-keyword">int</span> <span class="hljs-title">size</span><span class="hljs-params">()</span> <span class="hljs-comment">// 获取Map集合中键值对的个数。</span></span><br><span class="hljs-function">Collection&lt;V&gt; <span class="hljs-title">values</span><span class="hljs-params">()</span> <span class="hljs-comment">// 获取Map集合中所有的value，返回一个Collection</span></span><br><span class="hljs-function">Set&lt;K&gt; <span class="hljs-title">keySet</span><span class="hljs-params">()</span> <span class="hljs-comment">// 获取Map集合所有的key（所有的键是一个set集合）</span></span><br><span class="hljs-function">Set&lt;Map.Entry&lt;K,V&gt;&gt; <span class="hljs-title">entrySet</span><span class="hljs-params">()</span> <span class="hljs-comment">// 将Map集合转换成Set集合</span></span><br><span class="hljs-function"></span><br><span class="hljs-function"> 假设现在有一个Map集合，如下所示：</span><br><span class="hljs-function">    map1集合对象</span><br><span class="hljs-function">    key             value</span><br><span class="hljs-function">    ----------------------------</span><br><span class="hljs-function">    1               zhangsan</span><br><span class="hljs-function">    2               lisi</span><br><span class="hljs-function">    3               wangwu</span><br><span class="hljs-function">    4               zhaoliu</span><br><span class="hljs-function">    </span><br><span class="hljs-function">    Set set </span>= map1.entrySet();<br>	set集合对象：<br>    <span class="hljs-number">1</span>=zhangsan 【Set集合中元素的类型是 Map.Entry&lt;K,V&gt;】<br>    <span class="hljs-number">2</span>=lisi     【Map.Entry是静态内部类，是Map中的静态内部类】<br>    <span class="hljs-number">3</span>=wangwu<br>    <span class="hljs-number">4</span>=zhaoliu ---&gt; 这个东西是个什么？Map.Entry<br></code></pre></td></tr></table></figure>

<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 创建 Map 集合</span><br>Map&lt;Integer, String&gt; map = <span class="hljs-keyword">new</span> HashMap&lt;&gt;();<br><br><span class="hljs-comment">// V put(K key, V value)</span><br>map.put(<span class="hljs-number">1</span>, <span class="hljs-string">&quot;zhangsan&quot;</span>);<br>map.put(<span class="hljs-number">2</span>, <span class="hljs-string">&quot;lisi&quot;</span>);<br>map.put(<span class="hljs-number">3</span>, <span class="hljs-string">&quot;wangwu&quot;</span>);<br>map.put(<span class="hljs-number">4</span>, <span class="hljs-string">&quot;zhaoliu&quot;</span>);<br><br><span class="hljs-comment">// V get(Object key)</span><br>String value02 = map.get(<span class="hljs-number">2</span>);<br>System.out.println(<span class="hljs-string">&quot;key为2的value是&quot;</span> + value02 );<span class="hljs-comment">// key为2的value是lisi</span><br><br><span class="hljs-comment">// boolean containsKey(Object key)</span><br><span class="hljs-comment">// boolean containsValue(Object value)</span><br><span class="hljs-comment">// 底层都是使用 equals() 方法来进行判断的</span><br>System.out.println(<span class="hljs-string">&quot;map中是否包含key = 3，&quot;</span> + map.containsKey(<span class="hljs-number">3</span>));<span class="hljs-comment">// map中是否包含key = 3，true</span><br>System.out.println(<span class="hljs-string">&quot;map中是否包含key = 5，&quot;</span> + map.containsKey(<span class="hljs-number">5</span>));<span class="hljs-comment">// map中是否包含key = 5，false</span><br>System.out.println(<span class="hljs-string">&quot;map中是否包含value = zhangsan，&quot;</span> + map.containsValue(<span class="hljs-string">&quot;zhangsan&quot;</span>));<span class="hljs-comment">// map中是否包含value = zhangsan，true</span><br><br><span class="hljs-comment">// V remove(Object key)</span><br>String remove = map.remove(<span class="hljs-number">1</span>);<br>System.out.println(<span class="hljs-string">&quot;移除掉的value是：&quot;</span> + remove);<span class="hljs-comment">// 移除掉的value是：zhangsan</span><br><br><span class="hljs-comment">// Collection&lt;V&gt; values()</span><br>Collection&lt;String&gt; values = map.values();<br><span class="hljs-keyword">for</span> (String s : values) &#123;<br>    System.out.println(s);<br>&#125;<br><br><span class="hljs-comment">// Set&lt;K&gt; keySet()</span><br>Set&lt;Integer&gt; keys = map.keySet();<br><span class="hljs-keyword">for</span> (Integer i : keys) &#123;<br>    System.out.println(i);<br>&#125;<br><br><span class="hljs-comment">// Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()</span><br>Set&lt;Map.Entry&lt;Integer, String&gt;&gt; set = map.entrySet();<br>System.out.println(set);<span class="hljs-comment">// [2=lisi, 3=wangwu, 4=zhaoliu]</span><br><br><span class="hljs-comment">// void clear()</span><br>map.clear();<br><br><span class="hljs-comment">// int size()</span><br>System.out.println(map.size());<span class="hljs-comment">// 0</span><br></code></pre></td></tr></table></figure>

<p><strong>Map集合的编历：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 第一种：获取所有的Key，通过编历Key，来编历Value</span><br><span class="hljs-comment">// 效率较低，因为每次都要从 Map 集合中通过 key 去找 value</span><br>Set&lt;Integer&gt; keys = map.keySet();<br><span class="hljs-keyword">for</span> (Integer key : keys) &#123;<br>    System.out.println(key + <span class="hljs-string">&quot;=&quot;</span> + map.get(key));<br>&#125;<br><br><span class="hljs-comment">// 第二种：Set&lt;Map.Entry&lt;K,V&gt;&gt; entrySet()</span><br><span class="hljs-comment">// 将 Map 转换为 Set，Set 中存储的数据的数据类型是 Map.Entry</span><br>Set&lt;Map.Entry&lt;Integer, String&gt;&gt; set = map.entrySet();<br><span class="hljs-keyword">for</span> (Map.Entry&lt;Integer, String&gt; node : set) &#123;<br>    System.out.println(node.getKey() + <span class="hljs-string">&quot;=&quot;</span> + node.getValue());<br>&#125;<br><br><span class="hljs-comment">// 第三种：JDK8新特性</span><br>map.forEach((key, value) -&gt;&#123;<br>    System.out.println(key + <span class="hljs-string">&quot;=&quot;</span> + value);<br>&#125;);<br></code></pre></td></tr></table></figure>



<h4 id="18-6-2-HashSet-HashMap"><a href="#18-6-2-HashSet-HashMap" class="headerlink" title="18.6.2 HashSet / HashMap"></a>18.6.2 HashSet / HashMap</h4><p><strong>HashMap集合底层是==哈希表/散列表==的数据结构</strong>。</p>
<p><strong>哈希表是一个怎样的数据结构呢：</strong></p>
<ul>
<li>哈希表是一个数组和单向链表的结合体。</li>
<li>数组：在查询方面效率很高，随机增删方面效率很低。</li>
<li>单向链表：在随机增删方面效率较高，在查询方面效率很低。</li>
<li>哈希表将以上的两种数据结构融合在一起，充分发挥它们各自的优点。(一维数组，这个数组中每一个元素是一个单向链表)</li>
</ul>
<p><img src="https://img-blog.csdnimg.cn/20210222185835378.png" srcset="/img/loading.gif" lazyload alt="008-哈希表或者散列表数据结构"></p>
<p><strong>HashMap集合底层的源代码：</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br></pre></td><td class="code"><pre><code class="hljs java">HashMap集合底层的源代码：<br><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">HashMap</span></span>&#123;<br>    <span class="hljs-comment">// HashMap底层实际上就是一个数组。（一维数组）</span><br>    Node&lt;K,V&gt;[] table;<br>    <span class="hljs-comment">// 静态的内部类HashMap.Node</span><br>    <span class="hljs-keyword">static</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Node</span>&lt;<span class="hljs-title">K</span>,<span class="hljs-title">V</span>&gt; </span>&#123;<br>        <span class="hljs-keyword">final</span> <span class="hljs-keyword">int</span> hash; <span class="hljs-comment">// 哈希值（哈希值是key的hashCode()方法的执行结果。hash值通过哈希函数/算法，可以转换存储成数组的下标。）</span><br>        <span class="hljs-keyword">final</span> K key; <span class="hljs-comment">// 存储到Map集合中的那个key</span><br>        V value; <span class="hljs-comment">// 存储到Map集合中的那个value</span><br>        Node&lt;K,V&gt; next; <span class="hljs-comment">// 下一个节点的内存地址。</span><br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>两个方法的实现原理：</strong></p>
<p><code>map.put(k, v);</code></p>
<p>第一步：<strong>先将 k, v 封装到 Node 对象当中</strong></p>
<p>第二步：<strong>底层会调用 k 的 hashCode() 方法得出 hash 值</strong></p>
<p>第三步：通过哈希函数 / 哈希算法将 hash 值转换为数组的下标，如果该下标位置上没有元素，那么就直接将 Node 对象放到这个位置上了，如果该下标上有元素，就会那着 <strong>Node 节点上的 k 和链表中每个节点的 k 进行 equals</strong> ，如果所有 equals 方法返回的都是 false，那么这个新节点将会被添加到链表的最后，如果其中有一个返回 true，<strong>那么这个节点的 vlaue 值会被 Node 节点的 v 覆盖。</strong></p>
<p><code>v = map.get(k);</code></p>
<p>先调用 k 的 <strong>hashCode 方法取得 hash 值</strong>，然后通过<strong>哈希算法将 hash 值转换为 数组的下标</strong>，通过数组下标快速定位到某个位置，<strong>如果这个位置上什么也没有，就返回 null</strong>，如果这个位置上有单向链表，就拿着参数 k 和链表上各个节点的 k 进行 equals ，<strong>如果 equals 方法返回的都是 false，那么方法返回 null</strong>，只要其中有一个节点 equals <strong>后返回 true，此时这个节点上的 value 就是我们要找的 value</strong>，get 方法最终返回这个 value。</p>
<p><strong>HashMap集合的key部分特点：==无序，不可重复==</strong></p>
<ul>
<li>无序：因为不一定挂到哪个单向链表上；</li>
<li>不可重复：equals 方法保证  HashMap 集合的 key 不可重复，如果 key 重复了，value 就会覆盖。</li>
</ul>
<p><strong>==重点：放在HashMap集合key部分的元素，以及放在HashSet集合中的元素，需要同时重写hashCode和equals方法。==</strong></p>
<p><img src="C:/Users/30117/AppData/Roaming/Typora/typora-user-images/image-20210222191852249.png" srcset="/img/loading.gif" lazyload alt="image-20210222191852249"></p>
<p>**HashMap 集合的==默认初始化容量是16，默认加载因子是0.75==**（这个默认加载因子是当HashMap集合底层数组的容量达到75%的时候，数组开始扩容。）</p>
<p>重点，<strong>记住：HashMap集合初始化容量必须是2的倍数</strong>，这也是官方推荐的，这是因为达到散列均匀，为了提高HashMap集合的存取效率，所必须的。</p>
<p><strong>对于哈希表数据结构来说：</strong></p>
<ul>
<li><pre><code>如果o1和o2的hash值相同，一定是放到同一个单向链表上。
</code></pre>
</li>
<li><pre><code>当然如果o1和o2的hash值不同，但由于哈希算法执行结束之后转换的数组下标可能相同，此时会发生 **==“哈希碰撞”==**。
</code></pre>
</li>
</ul>
<p><strong>HashMap集合key部分允许null吗：允许</strong></p>
<ul>
<li>但是要注意：HashMap集合的key null值只能有一个。</li>
</ul>
<h4 id="18-6-3-HashTable"><a href="#18-6-3-HashTable" class="headerlink" title="18.6.3 HashTable"></a>18.6.3 HashTable</h4><p>Hashtable方法都带有synchronized：线程安全的。</p>
<p>Hashtable和HashMap一样，底层都是哈希表数据结构。</p>
<p>Hashtable的<strong>初始化容量是11，默认加载因子是：0.75f</strong></p>
<p><strong>Hashtable的扩容是：原容量 * 2 + 1</strong></p>
<p>Hashtable的key和value都是不能为null的。（HashMap集合的key和value都是可以为null的。）</p>
<h4 id="18-6-4-Properties"><a href="#18-6-4-Properties" class="headerlink" title="18.6.4 Properties"></a>18.6.4 Properties</h4><p><strong>Properties是一个Map集合，继承Hashtable，Properties的==key和value都是String类型==。</strong></p>
<p>Properties被称为属性类对象</p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-comment">// 创建一个Properties对象</span><br>Properties pro = <span class="hljs-keyword">new</span> Properties();<br><br><span class="hljs-comment">// 两个方法，一个存，一个取</span><br>pro.setProperty(<span class="hljs-string">&quot;11&quot;</span>, <span class="hljs-string">&quot;AA&quot;</span>);<br>pro.setProperty(<span class="hljs-string">&quot;22&quot;</span>, <span class="hljs-string">&quot;BB&quot;</span>);<br>pro.setProperty(<span class="hljs-string">&quot;33&quot;</span>, <span class="hljs-string">&quot;CC&quot;</span>);<br>pro.setProperty(<span class="hljs-string">&quot;44&quot;</span>, <span class="hljs-string">&quot;DD&quot;</span>);<br>pro.setProperty(<span class="hljs-string">&quot;55&quot;</span>, <span class="hljs-string">&quot;EE&quot;</span>);<br><br><span class="hljs-comment">// 通过key获取value</span><br>System.out.println(pro.getProperty(<span class="hljs-string">&quot;11&quot;</span>));<br>System.out.println(pro.getProperty(<span class="hljs-string">&quot;22&quot;</span>));<br>System.out.println(pro.getProperty(<span class="hljs-string">&quot;33&quot;</span>));<br>System.out.println(pro.getProperty(<span class="hljs-string">&quot;44&quot;</span>));<br>System.out.println(pro.getProperty(<span class="hljs-string">&quot;55&quot;</span>));<br></code></pre></td></tr></table></figure>



<h4 id="18-6-5-TreeSet-TreeMap"><a href="#18-6-5-TreeSet-TreeMap" class="headerlink" title="18.6.5 TreeSet / TreeMap"></a>18.6.5 TreeSet / TreeMap</h4><p><strong>TreeSet集合存储元素特点：</strong></p>
<ul>
<li>无序不可重复的，但是存储的元素可以自动按照大小顺序排序！</li>
<li>无序：这里的无序指的是存进去的顺序和取出来的顺序不同。并且没有下标。</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br></pre></td><td class="code"><pre><code class="hljs java">TreeSet&lt;String&gt; treeSet = <span class="hljs-keyword">new</span> TreeSet&lt;&gt;();<br>treeSet.add(<span class="hljs-string">&quot;abd&quot;</span>);<br>treeSet.add(<span class="hljs-string">&quot;aba&quot;</span>);<br>treeSet.add(<span class="hljs-string">&quot;abc&quot;</span>);<br><span class="hljs-keyword">for</span> (String s : treeSet) &#123;<br>    System.out.println(s);<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">	输出：</span><br><span class="hljs-comment">		aba</span><br><span class="hljs-comment">        abc</span><br><span class="hljs-comment">        abd</span><br><span class="hljs-comment">*/</span><br></code></pre></td></tr></table></figure>

<p><strong>TreeSet集合底层实际上是一个TreeMap，TreeMap集合底层是一个二叉树。</strong></p>
<p><img src="https://img-blog.csdnimg.cn/20210222213937898.png" srcset="/img/loading.gif" lazyload alt="010-自平衡二叉树"></p>
<p><strong>放到TreeSet集合中的元素，等同于放到TreeMap集合key部分了。</strong></p>
<p><strong>对自定义的类型来说，TreeSet可以排序吗？</strong></p>
<ul>
<li><p>以下程序中对于Person类型来说，无法排序。因为没有指定Person对象之间的比较规则。</p>
</li>
<li><p>谁大谁小并没有说明啊。</p>
</li>
<li><p>以下程序运行的时候出现了这个异常：</p>
</li>
<li><p><code>java.lang.ClassCastException:class com.bjpowernode.javase.collection.Personcannot be cast to class java.lang.Comparable</code></p>
</li>
<li><p>出现这个异常的原因是：Person类没有实现 <code>java.lang.Comparable </code>接口。</p>
</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        TreeSet&lt;Person&gt; set = <span class="hljs-keyword">new</span> TreeSet&lt;&gt;();<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">12</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">8</span>));<br>        <span class="hljs-keyword">for</span> (Person person : set) &#123;<br>            System.out.println(person);<br>        &#125;<br>    &#125;<br>&#125;<br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> </span>&#123;<br>    <span class="hljs-keyword">int</span> age;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span><span class="hljs-params">(<span class="hljs-keyword">int</span> age)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.age = age;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Person&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;age=&quot;</span> + age +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>TreeSet集合中元素可排序的第一种方式：实现 <code>java.lang.Comparable</code> 接口，重写 <code>compareTo()</code> 方法</strong></p>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br><span class="line">26</span><br><span class="line">27</span><br><span class="line">28</span><br><span class="line">29</span><br><span class="line">30</span><br><span class="line">31</span><br><span class="line">32</span><br><span class="line">33</span><br><span class="line">34</span><br><span class="line">35</span><br><span class="line">36</span><br><span class="line">37</span><br><span class="line">38</span><br><span class="line">39</span><br><span class="line">40</span><br><span class="line">41</span><br><span class="line">42</span><br><span class="line">43</span><br><span class="line">44</span><br><span class="line">45</span><br><span class="line">46</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        TreeSet&lt;Person&gt; set = <span class="hljs-keyword">new</span> TreeSet&lt;&gt;();<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsan&quot;</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsi&quot;</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">12</span>, <span class="hljs-string">&quot;lisi&quot;</span>));<br>        <span class="hljs-keyword">for</span> (Person person : set) &#123;<br>            System.out.println(person);<br>        &#125;<br>        <span class="hljs-comment">/*</span><br><span class="hljs-comment">        	输出：</span><br><span class="hljs-comment">        		Person&#123;age=12, name=&#x27;lisi&#x27;&#125;</span><br><span class="hljs-comment">                Person&#123;age=20, name=&#x27;zhangsan&#x27;&#125;</span><br><span class="hljs-comment">                Person&#123;age=20, name=&#x27;zhangsi&#x27;&#125;</span><br><span class="hljs-comment">        */</span><br>    &#125;<br>&#125;<br><br><span class="hljs-comment">// 要比较的类实现 Comparable 接口</span><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Person</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Comparable</span>&lt;<span class="hljs-title">Person</span>&gt;</span>&#123;<br>    <span class="hljs-keyword">int</span> age;<br>    String name;<br><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-title">Person</span><span class="hljs-params">(<span class="hljs-keyword">int</span> age, String name)</span> </span>&#123;<br>        <span class="hljs-keyword">this</span>.age = age;<br>        <span class="hljs-keyword">this</span>.name = name;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> String <span class="hljs-title">toString</span><span class="hljs-params">()</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-string">&quot;Person&#123;&quot;</span> +<br>                <span class="hljs-string">&quot;age=&quot;</span> + age +<br>                <span class="hljs-string">&quot;, name=&#x27;&quot;</span> + name + <span class="hljs-string">&#x27;\&#x27;&#x27;</span> +<br>                <span class="hljs-string">&#x27;&#125;&#x27;</span>;<br>    &#125;<br><br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-comment">// 先按照年龄升序，如果年龄一样的再按照姓名升序。</span><br>    <span class="hljs-comment">// compareTo方法的返回值很重要：</span><br>    <span class="hljs-comment">// 返回0表示相同，value会覆盖。</span><br>    <span class="hljs-comment">// 返回&gt;0，会继续在右子树上找。</span><br>    <span class="hljs-comment">// 返回&lt;0，会继续在左子树上找。</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compareTo</span><span class="hljs-params">(Person o)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> <span class="hljs-keyword">this</span>.age &gt; o.age ? (Integer.compare(<span class="hljs-keyword">this</span>.age, o.age)) : (<span class="hljs-keyword">this</span>.name.compareTo(o.name));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<p><strong>TreeSet集合中元素可排序的第二种方式：使用比较器的方式</strong>。</p>
<ul>
<li>可以写一个类实现比较器接口 <code>java.util.Comparator</code> ，重写 <code>compare()</code> 方法，将比较器作为参数放入 TreeSet 的构造方法中</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br><span class="line">19</span><br><span class="line">20</span><br><span class="line">21</span><br><span class="line">22</span><br><span class="line">23</span><br><span class="line">24</span><br><span class="line">25</span><br></pre></td><td class="code"><pre><code class="hljs java"><span class="hljs-keyword">public</span> <span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">Test</span> </span>&#123;<br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">static</span> <span class="hljs-keyword">void</span> <span class="hljs-title">main</span><span class="hljs-params">(String[] args)</span> </span>&#123;<br>        TreeSet&lt;Person&gt; set = <span class="hljs-keyword">new</span> TreeSet&lt;&gt;(<span class="hljs-keyword">new</span> PersonComparator());<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsan&quot;</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsi&quot;</span>));<br>        set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">12</span>, <span class="hljs-string">&quot;lisi&quot;</span>));<br>        <span class="hljs-keyword">for</span> (Person person : set) &#123;<br>            System.out.println(person);<br>        &#125;<br>        <span class="hljs-comment">/*</span><br><span class="hljs-comment">        	输出：</span><br><span class="hljs-comment">        		Person&#123;age=12, name=&#x27;lisi&#x27;&#125;</span><br><span class="hljs-comment">                Person&#123;age=20, name=&#x27;zhangsan&#x27;&#125;</span><br><span class="hljs-comment">                Person&#123;age=20, name=&#x27;zhangsi&#x27;&#125;</span><br><span class="hljs-comment">        */</span><br>    &#125;<br>&#125;<br><br><br><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">PersonComparator</span> <span class="hljs-keyword">implements</span> <span class="hljs-title">Comparator</span>&lt;<span class="hljs-title">Person</span>&gt; </span>&#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compare</span><span class="hljs-params">(Person o1, Person o2)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> o1.age &gt; o2.age ? (Integer.compare(o1.age, o2.age)) : (o1.name.compareTo(o2.name));<br>    &#125;<br>&#125;<br></code></pre></td></tr></table></figure>

<ul>
<li>可以使用匿名内部类写一个比较器</li>
</ul>
<figure class="highlight java"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br><span class="line">9</span><br><span class="line">10</span><br><span class="line">11</span><br><span class="line">12</span><br><span class="line">13</span><br><span class="line">14</span><br><span class="line">15</span><br><span class="line">16</span><br><span class="line">17</span><br><span class="line">18</span><br></pre></td><td class="code"><pre><code class="hljs java">TreeSet&lt;Person&gt; set = <span class="hljs-keyword">new</span> TreeSet&lt;&gt;(<span class="hljs-keyword">new</span> Comparator&lt;Person&gt;() &#123;<br>    <span class="hljs-meta">@Override</span><br>    <span class="hljs-function"><span class="hljs-keyword">public</span> <span class="hljs-keyword">int</span> <span class="hljs-title">compare</span><span class="hljs-params">(Person o1, Person o2)</span> </span>&#123;<br>        <span class="hljs-keyword">return</span> o1.age &gt; o2.age ? (Integer.compare(o1.age, o2.age)) : (o1.name.compareTo(o2.name));<br>    &#125;<br>&#125;);<br>set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsan&quot;</span>));<br>set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">20</span>, <span class="hljs-string">&quot;zhangsi&quot;</span>));<br>set.add(<span class="hljs-keyword">new</span> Person(<span class="hljs-number">12</span>, <span class="hljs-string">&quot;lisi&quot;</span>));<br><span class="hljs-keyword">for</span> (Person person : set) &#123;<br>    System.out.println(person);<br>&#125;<br><span class="hljs-comment">/*</span><br><span class="hljs-comment">输出：</span><br><span class="hljs-comment">	Person&#123;age=12, name=&#x27;lisi&#x27;&#125;</span><br><span class="hljs-comment">    Person&#123;age=20, name=&#x27;zhangsan&#x27;&#125;</span><br><span class="hljs-comment">    Person&#123;age=20, name=&#x27;zhangsi&#x27;&#125;</span><br><span class="hljs-comment"> */</span><br></code></pre></td></tr></table></figure>

<p><strong>Comparable 和 Comparator 怎么选择呢？</strong></p>
<ul>
<li><strong>当比较规则不会发生改变的时候</strong>，或者说当比较规则只有1个的时候，建议实现Comparable接口。</li>
<li><strong>如果比较规则有多个，并且需要多个比较规则之间频繁切换</strong>，建议使用Comparator接口。（<strong>Comparator接口的设计符合OCP原则。</strong>）</li>
</ul>

            </div>
            <hr>
            <div>
              <div class="post-metas mb-3">
                
                  <div class="post-meta mr-3">
                    <i class="iconfont icon-category"></i>
                    
                      <a class="hover-with-bg" href="/categories/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
                
                  <div class="post-meta">
                    <i class="iconfont icon-tags"></i>
                    
                      <a class="hover-with-bg" href="/tags/Java-%E5%9F%BA%E7%A1%80/">Java 基础</a>
                    
                  </div>
                
              </div>
              
                <p class="note note-warning">
                  
                    本博客所有文章除特别声明外，均采用 <a target="_blank" href="https://creativecommons.org/licenses/by-sa/4.0/deed.zh" rel="nofollow noopener noopener">CC BY-SA 4.0 协议</a> ，转载请注明出处！
                  
                </p>
              
              
                <div class="post-prevnext">
                  <article class="post-prev col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-IO-%E6%B5%81/">
                        <i class="iconfont icon-arrowleft"></i>
                        <span class="hidden-mobile">19、IO 流</span>
                        <span class="visible-mobile">上一篇</span>
                      </a>
                    
                  </article>
                  <article class="post-next col-6">
                    
                    
                      <a href="/2021/09/13/Java%E5%9F%BA%E7%A1%80-2021-09-13-%E5%BC%82%E5%B8%B8%EF%BC%88Exception%EF%BC%89/">
                        <span class="hidden-mobile">17、异常（Exception）</span>
                        <span class="visible-mobile">下一篇</span>
                        <i class="iconfont icon-arrowright"></i>
                      </a>
                    
                  </article>
                </div>
              
            </div>

            
          </article>
        </div>
      </div>
    </div>
    
      <div class="d-none d-lg-block col-lg-2 toc-container" id="toc-ctn">
        <div id="toc">
  <p class="toc-header"><i class="iconfont icon-list"></i>&nbsp;目录</p>
  <div class="toc-body" id="toc-body"></div>
</div>

      </div>
    
  </div>
</div>

<!-- Custom -->


    

    
      <a id="scroll-top-button" href="#" role="button">
        <i class="iconfont icon-arrowup" aria-hidden="true"></i>
      </a>
    

    
      <div class="modal fade" id="modalSearch" tabindex="-1" role="dialog" aria-labelledby="ModalLabel"
     aria-hidden="true">
  <div class="modal-dialog modal-dialog-scrollable modal-lg" role="document">
    <div class="modal-content">
      <div class="modal-header text-center">
        <h4 class="modal-title w-100 font-weight-bold">搜索</h4>
        <button type="button" id="local-search-close" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body mx-3">
        <div class="md-form mb-5">
          <input type="text" id="local-search-input" class="form-control validate">
          <label data-error="x" data-success="v"
                 for="local-search-input">关键词</label>
        </div>
        <div class="list-group" id="local-search-result"></div>
      </div>
    </div>
  </div>
</div>
    

    
  </main>

  <footer class="text-center mt-5 py-3">
  <div class="footer-content">
     <a href="https://hexo.io" target="_blank" rel="nofollow noopener"><span>Hexo</span></a> <i class="iconfont icon-love"></i> <a href="https://github.com/fluid-dev/hexo-theme-fluid" target="_blank" rel="nofollow noopener"><span>Fluid</span></a> 
  </div>
  

  

  
</footer>


  <!-- SCRIPTS -->
  
  <script  src="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.js" ></script>
  <link  rel="stylesheet" href="https://cdn.jsdelivr.net/npm/nprogress@0.2.0/nprogress.min.css" />

  <script>
    NProgress.configure({"showSpinner":false,"trickleSpeed":100})
    NProgress.start()
    window.addEventListener('load', function() {
      NProgress.done();
    })
  </script>


<script  src="https://cdn.jsdelivr.net/npm/jquery@3.6.0/dist/jquery.min.js" ></script>
<script  src="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/js/bootstrap.min.js" ></script>
<script  src="/js/events.js" ></script>
<script  src="/js/plugins.js" ></script>

<!-- Plugins -->


  
    <script  src="/js/img-lazyload.js" ></script>
  



  



  <script  src="https://cdn.jsdelivr.net/npm/tocbot@4.12.3/dist/tocbot.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/@fancyapps/fancybox@3.5.7/dist/jquery.fancybox.min.js" ></script>



  <script  src="https://cdn.jsdelivr.net/npm/anchor-js@4.3.1/anchor.min.js" ></script>



  <script defer src="https://cdn.jsdelivr.net/npm/clipboard@2.0.8/dist/clipboard.min.js" ></script>



  <script  src="/js/local-search.js" ></script>






  <script  src="https://cdn.jsdelivr.net/npm/typed.js@2.0.12/lib/typed.min.js" ></script>
  <script>
    (function (window, document) {
      var typing = Fluid.plugins.typing;
      var title = document.getElementById('subtitle').title;
      
      typing(title)
      
    })(window, document);
  </script>














  
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/xiaoxuehua.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/caidai.js"></script>
<script src="//cdn.jsdelivr.net/gh/bynotes/texiao/source/js/love.js"></script>
<script src="//cdn.jsdelivr.net/npm/aplayer/dist/APlayer.min.js"></script>
<script src="//cdn.jsdelivr.net/gh/metowolf/MetingJS@1.2/dist/Meting.min.js"></script>



<!-- 主题的启动项 保持在最底部 -->
<script  src="/js/boot.js" ></script>

  <!--动态线条背景-->
  <script type="text/javascript" color="70,119,184" opacity='1' zIndex="-2" count="200" src="//cdn.bootcss.com/canvas-nest.js/1.0.0/canvas-nest.min.js"></script>

<script src="/live2dw/lib/L2Dwidget.min.js?094cbace49a39548bed64abff5988b05"></script><script>L2Dwidget.init({"model":{"jsonPath":"/live2dw/assets/koharu.model.json"},"display":{"position":"left","width":200,"height":400},"mobile":{"show":false},"log":false,"pluginJsPath":"lib/","pluginModelPath":"assets/","pluginRootPath":"live2dw/","tagMode":false});</script></body>
</html>
